package com.leng.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: 冷俊杰
 * @Description: TODO
 * @DateTime: 2022/7/8 13:30
 **/
public class SqlSessionUtils {
    private static SqlSessionFactory build;

    static {
        //使用mybatis第一步，获取sqlsessionfactoryl对象
        try {
            String resource = "mybatis-config.xml";
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {
        return build.openSession(true);
    }
}